<?php
namespace jy_shouhou;
defined('BY_JYA') or exit('error');
/*
  师傅订单
	
*/
class order extends \plugin{
	/*
		系统指派，shouhou_id是MID
	*/
	public function order_pai($order){
		$order_id=intval($order['id']);
		$allusers=array();
		if($order['storeid'] && !$order['merchid']){
			$areas=pdo_getall('jy_shouhou_area',array('store_id'=>$order['storeid']));
			foreach($areas as &$item){
				$users=pdo_getall('jy_shouhou_member',array('group_id'=>$item['id'],'status'=>1),'mid');
				foreach($users as $uv){
					$allusers[]=$uv;
				}
			}
		}else{
			$users=pdo_getall('jy_shouhou_member',array('status'=>1),'mid');
			foreach($users as $uv){
					$allusers[]=$uv;
			}
		}
		$nums=count($allusers);
		$key=rand(0,$nums-1);
		$key=$key?:0;
		$shouhou_id=$allusers[$key]['mid'];
		if(!$shouhou_id){
			return false;
		}
		$params=array('order_id'=>$order_id,'shouhou_id'=>$shouhou_id);
		$this->order_sendget($params);
		return true;
	}
	/*
		抢单shouhou_id是MID
	*/
	public function order_get($params){
		$order_id=intval($params['order_id']);
		$shouhou_id=intval($params['shouhou_id']);
		//兼容错误，传过来的是ID而不是MID的时候！
		$mid=pdo_getcolumn('jy_shouhou_member',array('mid'=>$shouhou_id,'uniacid'=>$_SESSION['uniacid']),'mid');
		if(!$mid){
			$shouhou_id=pdo_getcolumn('jy_shouhou_member',array('id'=>$shouhou_id),'mid');
		}
		$isget=pdo_getcolumn('jy_weishop_order',array('id'=>$order_id),'status');
		if($isget==2){
			exi('该订单已被别人抢单了！','error');
		}
		pdo_update('jy_weishop_order',array('shouhou_id'=>$shouhou_id,'status'=>2,'send_status'=>1),array('id'=>$order_id));
		$this->create_shoulog($order_id,$shouhou_id,1);
		$this->model('notice')->notice_orderget($order_id);
		model('jy_weishop/order')->order_send($order_id,$params,true);
		return true;
	}
	/*
		发送订单给师傅
	*/
	public function order_sendget($params){
		$order_id=intval($params['order_id']);
		$shouhou_id=intval($params['shouhou_id']);
		$mid=pdo_getcolumn('jy_shouhou_member',array('mid'=>$shouhou_id,'uniacid'=>$_SESSION['uniacid']),'mid');
		pdo_update('jy_weishop_order',array('status'=>11),array('id'=>$order_id));
		$this->create_shoulog($order_id,$shouhou_id);
		return true;
	}
	/*
		师傅确定接单
	*/
	public function order_checkget($order_id){
		pdo_update('jy_weishop_order',array('status'=>12),array('id'=>$order_id));
		$this->up_shoulog($order_id,$_SESSION['uid'],2);
		$this->model('notice')->notice_orderget($order_id);
		model('jy_weishop/order')->order_send($order_id);
		return true;
	}
	/*
		转单超时
	*/
	public function order_checktime($order_id=0){
		$setting=$this->plugin_setting('setting');
		$send_status13_time=intval($setting['send_status13_time']);
		if(!$send_status13_time){
			return false;
		}
		if($order_id){
			$shoulog=pdo_get('jy_shouhou_shoulog',array('status'=>0,'mid'=>$_SESSION['uid'],'order_id'=>$order_id));
			if($shoulog['createtime']){
				$lasttime=$shoulog['createtime']+$send_status13_time*60;
				if($lasttime<TIMESTAMP){
					pdo_update('jy_weishop_order',array('status'=>15),array('id'=>$order_id));
					$this->up_shoulog($order_id,$shoulog['mid'],3);
				}
			}
		}else{
			$shoulogs=pdo_getall('jy_shouhou_shoulog',array('status'=>0,'uniacid'=>$_SESSION['uniacid']));
			foreach($shoulogs as $shoulog){
				$order_id=$shoulog['order_id'];
				if($shoulog['createtime']){
					$setting=$this->plugin_setting('setting');
					$lasttime=$shoulog['createtime']+$send_status13_time*60;
					if($lasttime<TIMESTAMP){
						pdo_update('jy_weishop_order',array('status'=>15),array('id'=>$order_id));
						$this->up_shoulog($order_id,$shoulog['mid'],3);
					}
				}
			}
		}
		return true;
	}
	/*
		取消订单
	*/
	public function unreceive($logid){
		$order_id=pdo_getcolumn('jy_shouhou_shoulog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$logid),'order_id');
		pdo_update('jy_weishop_order',array('status'=>15),array('id'=>$order_id));
		$this->model('order')->up_shoulog($order_id,'',3);
		return true;
	}
	/*
		完成订单
	*/
	public function order_receive($order_id){
		$this->create_orderlog($order_id);
		pdo_update('jy_weishop_order',array('status'=>16),array('id'=>$order_id));
		model('jy_weishop/order')->order_receive($order_id);
		$this->model('order')->up_shoulog($order_id,'',2);
		return true;
	}
	/*
		接单日志
	*/
	public function create_shoulog($order_id,$mid=0,$status=0){
		$mid=$mid?:$_SESSION['uid'];
		$data=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$mid,
				'order_id'=>$order_id,
				'status'=>$status,
				'createtime'=>TIMESTAMP,
		);
		$this->insert('shoulog',$data);
		return true;
	}
	/*
		uplog
	*/
	public function up_shoulog($order_id,$mid=0,$status=0){
		$mid=$mid?:$_SESSION['uid'];
		$this->pdo_update('shoulog',array('status'=>$status),array('mid'=>$mid,'order_id'=>$order_id,'status <>'=>3,'uniacid'=>$_SESSION['uniacid']));
		return true;
	}
	/*
		创建结算订单
	*/
	function create_orderlog($order_id,$mid=0){
		$order=pdo_get('jy_weishop_order',array('id'=>$order_id),array('ordersn','price'));
		//按商品/按件
		if($this->setting['shouhou_commission_type']){
			$get_price=$get_money=$this->setting['shouhou_fee'];
		}else{
			$get_price=$get_money=$this->setting['shouhou_fee'];
		}
		$mid=$mid?:$_SESSION['uid'];
		$data=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$mid,
				'ordersn'=>$order['ordersn'],
				'order_price'=>$order['price'],
				'get_price'=>$get_price,
				'get_money'=>$get_money,
				'createtime'=>TIMESTAMP,
				'status'=>0,
		);
		$this->insert('orderlog',$data);
		return true;
	}
	/*
		订单升级为待结算
	*/
	public function cash_orderlog(){
		$cashdays=$this->setting['cash_day']?:1;
		$cashtime = mktime(0,0,0,date('m'),date('d')-$cashdays,date('Y'));
		$orderlog=pdo_getall('jy_shouhou_orderlog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>0,'createtime <'=>$cashtime));
		foreach($orderlog as $item){
			pdo_update('jy_shouhou_orderlog',array('status'=>1),array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
		}
		return true;
	}
	/*
		待结算订单升级为结算完成/未结算
	*/
	public function change_orderlog($mid,$status=0){
		$orderlog=pdo_getall('jy_shouhou_orderlog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid,'status'=>1));
		foreach($orderlog as $item){
			pdo_update('jy_shouhou_orderlog',array('status'=>1),array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid'],'status'=>$status));
		}
		return true;
	}
}